// views/index/shop/mailservice/second/index.js
var BASE_URL = getApp().globalData.BASE_URL
var area = require('../../../../../utils/area');
var util = require('../../../../../utils/util');

import Notify from '../../../../../miniprogram_npm/@vant/weapp/notify/notify';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: '',
    pinyin: '',
    telNumber: '',
    iban: '',
    bic: '',
    kontonummer: '',
    bankleitzahl: '',
    firma: '',
    verwendungszweck: '',
    number: '',
    email: '',
    wechat_num: '',
    huilv: "",
    euro_price: 0,
    fuwufei: 0,
    amount: 0,
    ifma: false,
    ifhao: false
  },


  //填写钱数
  change_price: function (e) {
    console.log(e)
    if (this.ifnumber(+(e.detail)) && e.detail !== '') {
      if (e.detail > 500) {
        wx.showToast({
          title: '汇款金额不能大于500欧！',
          icon: 'none'
        })
        this.setData({
          number: 500,
          euro_price: 500,
          fuwufei: 50,
          amount:(550*this.data.huilv).toFixed(2)
        })
      } else {
        this.setData({
          number: e.detail,
          euro_price: e.detail,
          fuwufei: Math.ceil(e.detail / 50) * 5,
          amount: ((+e.detail+(Math.ceil(e.detail / 50) * 5))*this.data.huilv).toFixed(2)  
        })
      }
    } else {
      this.setData({
        number: e.detail,
        euro_price: 0,
        fuwufei: 0,
        amount:0
      })
    }
  },


  //是iban码还是账号
  change_order_one: function () {
    if (this.data.iban !== '' || this.data.bic !== '') {
      this.setData({
        ifhao: true
      })
    } else {
      this.setData({
        ifhao: false
      })
    }
    if (this.data.kontonummer !== '' || this.data.bankleitzahl !== '') {
      this.setData({
        ifma: true
      })
    } else {
      this.setData({
        ifma: false
      })
    }

  },






















  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var _this = this
    if (util.iflogin()) {
      var userinfo = wx.getStorageSync("USER")
      this.setData({
        email:userinfo.email
      })
      _this.gethuilv()
    }

    if (this.data.iban !== '') {
      console.log(11)
    }
  },
  //获得汇率
  gethuilv: function () {
    var _this = this
    wx.request({
      url: `${BASE_URL}/daihui/huilv`,
      method: "GET",
      header: {
        'Content-Type': 'application/json' //GET方式是这个
      },
      success(res) {
        console.log(res)
        _this.setData({
          huilv: res.data
        })
      }
    })
  },

  //填写信息的下一步按钮
  gothird: function () {
    var _this = this
    if (!_this.ifhanzi(_this.data.name)) {
      Notify('姓名输入错误');
    }

    if (!_this.ifnumber(+(_this.data.number)) || _this.data.number == '') {
      Notify('欧元请输入纯数字');
    }
    if (!_this.ifwewhat(_this.data.wechat_num)) {
      Notify('微信号输入错误');
    }
    if (!_this.codeVerification(_this.data.telNumber)) {
      Notify('请输入正确的手机号');
    }

    if (!_this.ifenglish(_this.data.pinyin)) {
      Notify('拼音输入错误');
    }

    if (_this.data.firma == '') {
      Notify('收款人名称不能为空');
    }
    if (_this.data.verwendungszweck == '') {
      Notify('汇款附言不能为空');
    }
    if (_this.data.number == '') {
      Notify('汇款金额不能为空');
    }

    var userinfo = wx.getStorageSync("USER")


    if (_this.ifhanzi(_this.data.name) && (
        _this.ifnumber(+(_this.data.number)) || _this.data.number !== '') &&
      _this.ifwewhat(_this.data.wechat) && _this.codeVerification(_this.data.telNumber) &&
      _this.ifenglish(_this.data.pinyin) && _this.data.firma !== '' && _this.data.verwendungszweck !== '' && _this.data.number !== ''
    ) {
      wx.request({
        url: `${BASE_URL}/daihui/info`,
        method: "POST",
        data: {
          name: _this.data.name,
          pinyin: _this.data.pinyin,
          telNumber: _this.data.telNumber,
          iban: _this.data.iban,
          bic: _this.data.bic,
          kontonummer: _this.data.kontonummer,
          bankleitzahl: _this.data.bankleitzahl,
          firma: _this.data.firma,
          verwendungszweck: _this.data.verwendungszweck,
          number: _this.data.number,
          email: _this.data.email,
          wechat_num: _this.data.wechat_num,
          type: 'xiaochengxv',
          uid: userinfo.id,
        },
        header: {
          'Content-Type': 'application/json' //GET方式是这个
        },
        success(res) {
          console.log(res)
          wx.navigateTo({
            url: `../sureorder/index?id=${res.data.id}`
          })
        }
      })
    }
  },














  //手机号正则
  codeVerification: function (phone) {
    let phoneCodeVerification = /^[1][3,4,5,7,8][0-9]{9}$/;
    return phoneCodeVerification.test(phone);
  },
  //欧元正则
  ifnumber: function (number) {
    let ifnumber = /^\d+(?=\.{0,1}\d+$|$)/;
    return ifnumber.test(number);
  },
  //英文字母正则
  ifenglish: function (pinyin) {
    let ifenglish = /^[A-Za-z]+$/;
    return ifenglish.test(pinyin);
  },
  //汉字正则
  ifhanzi: function (pinyin) {
    let ifhanzi = /^[\u4e00-\u9fa5]+$/;
    return ifhanzi.test(pinyin);
  },
  //微信号正则
  ifwewhat: function (pinyin) {
    let ifwewhat = /^[0-9a-zA-Z]*$/g;
    return ifwewhat.test(pinyin);
  },
  //邮箱验证
  ifemail: function (email) {
    let ifemail = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
    return ifemail.test(email);
  },



})